/*
 *
 *  Copyright (C) 2010 Tal Eisenberg
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package base;

import java.util.ArrayList;
import java.util.List;
import tal.drivechain.matchers.VarMatcher;
import tal.drivechain.plugin.Channel;
import tal.drivechain.plugin.Module;
import tal.drivechain.variable.types.ArrayType;
import tal.drivechain.variable.types.DataType;
import tal.drivechain.variable.SetProperty;
import tal.drivechain.variable.Variable;

/**
 *
 * @author Main
 */
public class Buffer extends Module {
    List buffer;
    VarMatcher addTrigger, releaseTrigger, clearTrigger;
    Variable maxSize = null;
    Variable out;
           
    @Override
    public void setupProperties() throws IllegalArgumentException {
        setAddTrigger(props.getProperty("addTrigger"), null);
        setReleaseTrigger(props.getProperty("releaseTrigger"), null);
        setClearTrigger(props.getProperty("clearTrigger"), null);
        
        if (props.hasProperty("maxSize"))
            setMaxSize(props.getProperty("maxSize"), null);
        
        buffer = new ArrayList();
        out = new Variable("buffer", new ArrayType(DataType.VAR), buffer, true);
    }

    public void setMaxSize(Object value, SetProperty.Flag flag) {
        maxSize = (Variable)value;
    } public Object getMaxSize() { return maxSize; }

    public void setAddTrigger(Object value, SetProperty.Flag flag) {
        addTrigger = (VarMatcher)value;
    } public Object getAddTrigger() { return addTrigger; }

    public void setReleaseTrigger(Object value, SetProperty.Flag flag) {
        releaseTrigger = (VarMatcher)value;
    } public Object getReleaseTrigger() { return releaseTrigger; }

    public void setClearTrigger(Object value, SetProperty.Flag flag) {
        clearTrigger = (VarMatcher)value;
    } public Object getClearTrigger() { return clearTrigger; }
    
    @Override
    protected void msgReceived(Variable in, long timestamp, Channel channel) {
        if (addTrigger.matches(in, channel, this)) {
            if (debugFlag)
                log("Adding " + in + " to buffer", MSG.DEBUG);

            buffer.add(in.getValue(this));
            if (maxSize!=null && (Integer)maxSize.getValue(this)<=buffer.size()) {
                if (debugFlag)
                    log("Buffer reached maximum size. ", MSG.DEBUG);
                sendBuffer();
            }            
        } if (releaseTrigger.matches(in, channel, this)) {
            if (debugFlag)
                log("Received release trigger. ", MSG.DEBUG);
            sendBuffer();
        } if (clearTrigger.matches(in, channel, this)) {
            if (debugFlag)
                log("Clearing buffer.", MSG.DEBUG);
            buffer.clear();
        } 
    }

    private void sendBuffer() {
        //send out and clear the buffer.
        if (infoFlag)
            log("Sending buffer.", MSG.INFO);
        else if (debugFlag)
            log("Sending buffer: " + out.toString(this), MSG.DEBUG);
        this.sendToOutputs(out, -1);
        buffer.clear();
    }
}
